<?php
namespace Adminx\Controller;

class GoodsController extends AdminController {
	public $delMid=2;
	public $db;
	public $category;
	
	public function _initialize(){
		parent::_initialize();
		//实例化数据模型
		$this->db=D("Goods");
		$this->category=D("Category");
	}
	#列表
	public function index() {
		$map['model']=$this->delMid;
		$cate = $this->category->where($map)->getField('id,name');
		$path = I('get.path');
		$keyword  = I('get.keyword');
		unset($map);
		if($path!=''){
			$map['path'] = array('like', $path.'%');
		}

		if($keyword!=''){
			$map['name'] = array('like', '%'.$keyword.'%');
		}
		$map['del']=0;
		$count = $this->db->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 10);
		$show = $page->show();
		$list = $this->db->where($map)->order('sort asc , id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

		unset($map);
		$map['model']=$this->delMid;
		$sclass = $this->category->field("id,name,fid,path")->where($map)->order('path')->select();
		foreach ($sclass as $key => $value) {
			$count = count(explode('-', $value['path'])) - 3;
			$sclass[$key]['count'] = $count;
		}
		$this->assign('sclass', $sclass);
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('cate', $cate);
		$this->assign('path', $path);
		$this->assign('keyword', $keyword);
		$this->display();
	}

	public function trash(){
		$cate = M('GoodsCate')->getField('id,name');

		$keyword  = I('get.keyword');

		if($keyword!=''){
			$map['name'] = array('like', '%'.$keyword.'%');
		}

		$map['del']=1;
		$obj = M('Goods');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 10);
		$show = $page->show();
		$list = $obj->where($map)->order('sort asc , id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('cate', $cate);
		$this->assign('keyword', $keyword);
		$this->display();
	}

	#添加
	public function add() {
		if ($_POST) {
			$this->all_add('Goods',U('Goods/index'));
		}else{
			$map['model']=$this->delMid;
			$cate = $this->category->field("id,name,fid,path")->where($map)->order('path')->select();	

			foreach ($cate as $key => $value) {
				$count = count(explode('-', $value['path'])) - 3;
				$cate[$key]['count'] = $count;
			}
			$this->assign('cate',$cate);
			$this->display();
		}
	}
	

	#编辑
	public function edit() {
		if($_POST){
			$this->all_save('Goods',U('Goods/index'));
		}else{
			$id = (int) $_GET['id'];
			if (!isset ($id)) {
				$this->error('参数错误');
			}
			$list = $this->db->where("id=$id")->find();
			if (!$list) {
				$this->error('信息不存在');
			} else {
				$map['model']=$this->delMid;
				$cate = $this->category->field("id,name,fid,path")->where($map)->order('path')->select();	
				foreach ($cate as $key => $value) {
					$count = count(explode('-', $value['path'])) - 3;
					$cate[$key]['count'] = $count;
				}

				if ($list['image']) {
	                $list['image'] = explode(",", $list['image']);
	            }

	            if ($list['carID']) {
	                $carID= explode(",", $list['carID']);
	            }

	            if ($list['brandID']) {
	                $brandID= explode(",", $list['brandID']);
	            }

				$map['brandID'] = array('in',$brandID);
				$car = M('Chexing')->where($map)->order('brandID asc')->select();

				$this->assign('cate',$cate);
				$this->assign('list', $list);
				$this->assign('brandID', $brandID);
				$this->assign('carID', $carID);
				$this->assign('car', $car);
				$this->display();
			}
		}
	}

	
	public function del(){
		$this->all_del('Goods','reload');
	}

	public function getChexing(){
		$bid = I('post.bid');
		if ($bid=='') {
			$arr = array('status'=>1,'data'=>array());
			echo json_encode($arr);die;
		}
		$bidArr = explode(",", $bid);
		$map['brandID'] = array('in',$bidArr);
		$list = M('Chexing')->where($map)->order('brandID asc')->select();
		$arr = array('status'=>1,'data'=>$list);
		echo json_encode($arr);
	}
}
?>